<?php
App::uses('AppController', 'Controller');
/**
 * Gifts Controller
 *
 * @property Gift $Gift
 */
class GiftsController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Gift->recursive = 0;
		$this->set('gifts', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Gift->exists($id)) {
			throw new NotFoundException(__('Invalid gift'));
		}
		$options = array('conditions' => array('Gift.' . $this->Gift->primaryKey => $id));
		$this->set('gift', $this->Gift->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Gift->create();
			if ($this->Gift->save($this->request->data)) {
				$this->Session->setFlash(__('The gift has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The gift could not be saved. Please, try again.'));
			}
		}
		$deliveries = $this->Gift->Delivery->find('list');
		$products = $this->Gift->Product->find('list');
		$this->set(compact('deliveries', 'products'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Gift->exists($id)) {
			throw new NotFoundException(__('Invalid gift'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Gift->save($this->request->data)) {
				$this->Session->setFlash(__('The gift has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The gift could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Gift.' . $this->Gift->primaryKey => $id));
			$this->request->data = $this->Gift->find('first', $options);
		}
		$deliveries = $this->Gift->Delivery->find('list');
		$products = $this->Gift->Product->find('list');
		$this->set(compact('deliveries', 'products'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Gift->id = $id;
		if (!$this->Gift->exists()) {
			throw new NotFoundException(__('Invalid gift'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Gift->delete()) {
			$this->Session->setFlash(__('Gift deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Gift was not deleted'));
		$this->redirect(array('action' => 'index'));
	}

/**
 * addgift method
 *
 * @return void
 */
	public function addgift($id = null) {
		if ($this->request->is('post')) {

			print_r($this->data);

			//check if there is any order quantity
			$empty = false;
			foreach($this->data['Gift'] as $i => $j):
				if(!empty($this->data['deliveredQty'][$i])){
					$empty = true;
				}
			endforeach;

			
			if(!$empty){
				$this->Session->setFlash(__('No gift has been received'),'default', array('class' => 'saveerror'));
				$this->redirect(array('controller' => 'deliveries','action' => 'view', $id));
			}
			else{
				//print_r($this->request->data);
				print_r('haha');
				$validate = true;
				foreach($this->data['deliveredQty'] as $i => $j){
					if(!empty($j)){
						$data = array('deliveredQty' => $j);
						$this->Gift->set($data);

						if ($this->Gift->validates()) {
						} else {
						    $validate = false;
						}
					}
				}

				$errors = $this->Gift->invalidFields();
				//print_r($errors);

				if(!$validate){
					$this->Session->setFlash(__('Please enter only positive numbers for quantity.'),'default', array('class' => 'saveerror'));
				}
				else {

					//save each gifts
					foreach($this->data['Gift'] as $i => $j):
						if(!empty($this->data['deliveredQty'][$i])){
							$data = array(	'products_id' 	=> $j,
											'deliveries_id'	=> $id,
											'deliveredQty'	=> $this->data['deliveredQty'][$i]);
							$this->Gift->create();
							if($this->Gift->save($data)){
								
							}
						}
					endforeach;

					$this->Session->setFlash(__('Gifts has been received'),'default', array('class' => 'savesuccess'));
					$this->redirect(array('controller' => 'deliveries','action' => 'view', $id));
				}
			}

			/*$this->Gift->create();
			if ($this->Gift->save($this->request->data)) {
				$this->Session->setFlash(__('The gift has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The gift could not be saved. Please, try again.'));
			}*/
		}
		$this->loadModel('Orders');
		$this->loadModel('Suppliers');
		$this->loadModel('Productsbrands');
		$this->loadModel('Productsranges');
		$this->loadModel('Productscategories');

		$delivery = $this->Gift->Delivery->findById($id);
		$order = $this->Orders->findById($delivery['Delivery']['orders_id']);
		$supplier = $this->Suppliers->findById($order['Orders']['suppliers_id']);
		$products = $this->Gift->Product->find('all', array('conditions' => array(	'archive' => '0',
																					'suppliers_id' => $supplier['Suppliers']['id'])));
		
		foreach ($products as $i => $j) {
				
			$brand = $this->Productsbrands->findById($j['Product']['productsbrands_id']);
			$products = Set::insert($products, $i.'.Brands', $brand['Productsbrands']);
			
			$ranges = $this->Productsranges->findById($j['Product']['productsranges_id']);
			if(empty($ranges)){$ranges = array('Productsranges' => array('name' => ''));}
			$products = Set::insert($products, $i.'.Ranges', $ranges['Productsranges']);
			
			$categories = $this->Productscategories->findById($j['Product']['productscategories_id']);
			if(empty($categories)){$categories = array('Productscategories' => array('name' => ''));}
			$products = Set::insert($products, $i.'.Categories', $categories['Productscategories']);
		}	

		$this->set(compact('delivery', 'products', 'supplier'));
	}
}
